package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.*;
import com.card.hwatt.service.*;
import com.card.hwatt.dto.*;

/**
 * 考勤
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/card")
public class CardController extends BaseController{

	@Autowired
	@Qualifier("cardService")
	private CardService cardService;

	@RequestMapping("/index")
	public String index() {
		return "card";
	}

	@RequestMapping("/loadAllByEmployee")
	public @ResponseBody
	PageResult<Card> loadAll(Long id, String startDate, String endDate,
			int page, int rows) {
		if (id == null) {
			PageResult<Card> p = new PageResult<Card>();
			p.rows = new ArrayList<Card>();
			return p;
		}

		String[] parsePatterns = { "yyyy-MM-dd", "yyyy-M-dd", "yyyy-M-d" };
		Date start = null;
		Date end = null;
		try {

			start = DateUtils.parseDate(startDate, parsePatterns);
			end = DateUtils.parseDate(endDate, parsePatterns);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(end);
			calendar.add(Calendar.DATE, 1);
			end = calendar.getTime();
		} catch (Exception e) {
			e.printStackTrace();
			PageResult<Card> p = new PageResult<Card>();
			p.rows = new ArrayList<Card>();
			return p;
		}

		List<Object> param = new ArrayList<Object>();
		param.add(id);
		param.add(new java.sql.Date(start.getTime()));
		param.add(new java.sql.Date(end.getTime()));
		PageResult<Card> result = CardAssembler.convert(cardService
				.loadAll(new PageParam(rows, page,
						" employee.id = ? and date >= ? and date < ? ", param,
						" date desc ")));
		return result;
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	PageResult<Card> loadAll(Integer id, String name, String startDate,
			String endDate, int page, int rows, String sort, String order) {

		if (id == null) {
			PageResult<Card> result = new PageResult<Card>();
			result.rows = new ArrayList<Card>();
			return result;
		}

		StringBuilder where = new StringBuilder(
				" employee.department.path like ? ");
		List<Object> param = new ArrayList<Object>();
		param.add("%@" + id + "@%");

		if (StringUtils.isNotEmpty(name)) {
			param.add("%" + name + "%");
			param.add("%" + name + "%");

			where.append(" and (employee.name like ? or employee.employeeCode like ? ) ");
		}

		if (StringUtils.isNotEmpty(startDate)
				&& StringUtils.isNotEmpty(endDate)) {
			String[] parsePatterns = { "yyyy-MM-dd", "yyyy-M-dd", "yyyy-M-d" };

			try {
				Date start = DateUtils.parseDate(startDate, parsePatterns);
				Date end = DateUtils.parseDate(endDate, parsePatterns);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(end);
				calendar.add(Calendar.DATE, 1);
				end = calendar.getTime();

				param.add(new java.sql.Date(start.getTime()));
				param.add(new java.sql.Date(end.getTime()));

				where.append(" and date >= ? and date < ? ");
			} catch (Exception e) {

			}
		}

		String orderBy = StringUtils.isEmpty(sort)
				|| StringUtils.isEmpty(order) ? " date desc " : sort + " "
				+ order;

		PageResult<Card> result = CardAssembler.convert(cardService
				.loadAll(new PageParam(rows, page, where.toString(), param,
						orderBy)));

		return result;
	}
}
